package com.future.mayi.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.baselib.view.CircleImageView;
import com.future.mayi.R;
import com.future.mayi.activity.AddressActivity;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.activity.FeedbackActivity;
import com.future.mayi.activity.GoodsAllOrderActivity;
import com.future.mayi.activity.InviteActivity;
import com.future.mayi.activity.LevelActivity;
import com.future.mayi.activity.MainActivity;
import com.future.mayi.activity.MyQrCodeActivity;
import com.future.mayi.activity.SettingActivity;
import com.future.mayi.activity.SystemMessageActivity;
import com.future.mayi.activity.UserInfoActivity;
import com.future.mayi.activity.WalletActivity;
import com.future.mayi.dialog.CommonDialog;
import com.future.mayi.dialog.KefuDialog;
import com.future.mayi.model.LevelupModel;
import com.future.mayi.model.OrderNumModel;
import com.future.mayi.model.SaleModel;
import com.future.mayi.model.UserInfoModel;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.utils.PriceUtils;
import com.future.mayi.utils.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class MyFragment extends BaseFragment {

    @BindView(R.id.tv_message_count)
    TextView tvMessageCount;
    @BindView(R.id.view_bar)
    View viewBar;
    @BindView(R.id.iv_message)
    ImageView ivMessage;
    @BindView(R.id.iv_level)
    ImageView ivLevel;
    @BindView(R.id.iv_head_img)
    CircleImageView ivHeadImg;
    @BindView(R.id.tv_nick_name)
    TextView tvNickName;
    @BindView(R.id.tv_wait_pay_count)
    TextView tvWaitPayCount;
    @BindView(R.id.tv_wait_send_count)
    TextView tvWaitSendCount;
    @BindView(R.id.tv_wait_get_count)
    TextView tvWaitGetCount;
    @BindView(R.id.tv_money_0)
    TextView tvMoney0;
    @BindView(R.id.tv_money_1)
    TextView tvMoney1;
    @BindView(R.id.tv_money_2)
    TextView tvMoney2;
    @BindView(R.id.tv_money_3)
    TextView tvMoney3;
    @BindView(R.id.tv_money_4)
    TextView tvMoney4;
    @BindView(R.id.tv_invite_code)
    TextView tvInviteCode;
    @BindView(R.id.tv_my_parent)
    TextView tvMyParent;
    @BindView(R.id.tv_invite_copy)
    TextView tvInviteCopy;
    @BindView(R.id.rl_my)
    LinearLayout rlMy;
    @BindView(R.id.tv_this_level)
    TextView tvThisLevel;
    @BindView(R.id.seek_bar)
    SeekBar seekBar;
    @BindView(R.id.tv_lack_price)
    TextView tvLackPrice;
    @BindView(R.id.tv_next_level)
    TextView tvNextLevel;
    @BindView(R.id.rl_level)
    View rlLevel;
    @BindView(R.id.ll_order_wait_pay)
    RelativeLayout llOrderWaitPay;
    @BindView(R.id.ll_order_wait_send)
    RelativeLayout llOrderWaitSend;
    @BindView(R.id.ll_order_wait_get)
    RelativeLayout llOrderWaitGet;
    @BindView(R.id.ll_order_all)
    LinearLayout llOrderAll;
    @BindView(R.id.tv_my_wallet)
    TextView tvMyWallet;
    @BindView(R.id.tv_money_1_hint)
    TextView tvMoney1Hint;
    @BindView(R.id.ll_money_1)
    LinearLayout llMoney1;
    @BindView(R.id.tv_money_2_hint)
    TextView tvMoney2Hint;
    @BindView(R.id.ll_money_2)
    LinearLayout llMoney2;
    @BindView(R.id.tv_money_3_hint)
    TextView tvMoney3Hint;
    @BindView(R.id.ll_money_3)
    LinearLayout llMoney3;
    @BindView(R.id.tv_money_4_hint)
    TextView tvMoney4Hint;
    @BindView(R.id.ll_money_4)
    LinearLayout llMoney4;
    @BindView(R.id.ll_money)
    LinearLayout llMoney;
    @BindView(R.id.iv_my_parent)
    ImageView ivMyParent;
    @BindView(R.id.ll_my_parent)
    LinearLayout llMyParent;
    @BindView(R.id.rl_invites)
    RelativeLayout rlInvites;
    @BindView(R.id.rl_1)
    TextView rl1;
    @BindView(R.id.rl_2)
    TextView rl2;
    @BindView(R.id.rl_3)
    TextView rl3;
    @BindView(R.id.rl_4)
    TextView rl4;
    @BindView(R.id.rl_5)
    TextView rl5;
    @BindView(R.id.rl_6)
    TextView rl6;
    @BindView(R.id.rl_7)
    TextView rl7;


    private int level;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        if (MainActivity.statusBar) {
            ViewGroup.LayoutParams layoutParams = viewBar.getLayoutParams();
            layoutParams.height = CommonUtils.getStatusBarHeight(getActivity());
        } else viewBar.setVisibility(View.GONE);
        seekBar.setClickable(false);
        seekBar.setEnabled(false);
        seekBar.setSelected(false);
        seekBar.setFocusable(false);
        setServiceView();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }


    public void refresh() {

    }


    @OnClick({R.id.iv_message, R.id.iv_head_img, R.id.iv_level, R.id.rl_my, R.id.ll_order_wait_pay, R.id.ll_order_wait_send, R.id.ll_order_wait_get, R.id.ll_order_refund, R.id.ll_order_all, R.id.rl_1, R.id.rl_2, R.id.rl_3, R.id.rl_4, R.id.rl_5, R.id.rl_6, R.id.rl_7, R.id.tv_invite_copy, R.id.ll_money_0, R.id.ll_money_1, R.id.ll_money_2, R.id.ll_money_3, R.id.ll_money_4, R.id.tv_money_0_hint, R.id.tv_money_1_hint, R.id.tv_money_2_hint, R.id.tv_money_3_hint, R.id.tv_money_4_hint, R.id.rl_level, R.id.tv_my_wallet, R.id.ll_my_parent})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_message:
                startActivity(new Intent(getContext(), SystemMessageActivity.class));
                break;
            case R.id.iv_head_img:
                startActivity(new Intent(getContext(), UserInfoActivity.class));
                break;
            case R.id.rl_level:
                break;
            case R.id.iv_level:
                startActivity(new Intent(getContext(), LevelActivity.class));
                break;
            case R.id.rl_my:
                startActivity(new Intent(getContext(), UserInfoActivity.class));
                break;
            case R.id.ll_order_wait_pay:
                GoodsAllOrderActivity.startActivity(getActivity(), 1);
                break;
            case R.id.ll_order_wait_send:
                GoodsAllOrderActivity.startActivity(getActivity(), 2);
                break;
            case R.id.ll_order_wait_get:
                GoodsAllOrderActivity.startActivity(getActivity(), 3);
                break;
            case R.id.ll_order_refund:
                GoodsAllOrderActivity.startActivity(getActivity(), 4);
                break;
            case R.id.ll_order_all:
                GoodsAllOrderActivity.startActivity(getActivity(), 0);
                break;
            case R.id.rl_1:
                if (level > 0) {
                    startActivity(new Intent(getActivity(), InviteActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), AddressActivity.class));
                }
                break;
            case R.id.rl_2:
                if (level > 0) {
                    startActivity(new Intent(mContext, MyQrCodeActivity.class));
                } else {
                    new KefuDialog(getActivity()).show(0);
                }
                break;
            case R.id.rl_3:
                if (level > 0) {
                    startActivity(new Intent(getActivity(), AddressActivity.class));
                } else {
                    new KefuDialog(getActivity()).show(1);
                }
                break;
            case R.id.rl_4:
                if (level > 0) {
                    new KefuDialog(getActivity()).show(0);
                } else {
                    startActivity(new Intent(getContext(), FeedbackActivity.class));
                }
                break;
            case R.id.rl_5:
                if (level > 0) {
                    new KefuDialog(getActivity()).show(1);
                } else {
                    startActivity(new Intent(getContext(), SettingActivity.class));
                }
                break;
            case R.id.rl_6:
                startActivity(new Intent(getContext(), FeedbackActivity.class));
                break;
            case R.id.rl_7:
                startActivity(new Intent(getContext(), SettingActivity.class));
                break;
            case R.id.tv_invite_copy:
                if (TextUtils.isEmpty(inviteCode)) return;
                CommonUtils.clipData(getContext(), inviteCode);
                toast("复制成功");
                break;
            case R.id.ll_money_0:
            case R.id.ll_money_1:
            case R.id.ll_money_2:
            case R.id.ll_money_3:
            case R.id.ll_money_4:
            case R.id.tv_my_wallet:
                startActivity(new Intent(getContext(), WalletActivity.class));
                break;
            case R.id.tv_money_0_hint:
                showHintDialog("统计当天所有已付款订单的佣金");
                break;
            case R.id.tv_money_1_hint:
                showHintDialog("统计本月1号起所有已付款订单的业绩（业绩=团购价-零售返佣）");
                break;
            case R.id.tv_money_2_hint:
                showHintDialog("统计本月1号起所有已付款订单的佣金");
                break;
            case R.id.tv_money_3_hint:
                showHintDialog("统计所有已确认收货订单的业绩（业绩=团购价-零售返佣）");
                break;
            case R.id.tv_money_4_hint:
                showHintDialog("统计所有已确认收货订单的佣金");
                break;
            case R.id.ll_my_parent:
                boolean isShowMyParent = !(boolean) SharedPreferencesUtils.get(getContext(), SharedPreferencesUtils.SpEnum.IsShowMyParent, true);
                SharedPreferencesUtils.set(getContext(), SharedPreferencesUtils.SpEnum.IsShowMyParent, isShowMyParent);
                ivMyParent.setImageResource(isShowMyParent ? R.drawable.img_my_parent_visi : R.drawable.img_my_parent_gone);
                if (isShowMyParent)
                    tvMyParent.setText(tvMyParent.getTag() == null ? "" : ((String) tvMyParent.getTag()));
                else tvMyParent.setText("****");
                break;
        }
    }


    private void showHintDialog(String content) {
        new CommonDialog.Builder(getActivity())
                .setCancelable(false)
                .setMessage(content)
                .setPositiveButton("我知道了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButtonColor(R.color.text_colorfe7712)
                .show();
    }


    public void showFragment() {
        onResume();
    }


    private void setLevel(int level, LevelupModel model) {
        this.level = level;
        setServiceView();
        int drawable = 0;
        int thisLevelDrawable = 0;
        int nextLevelDrawable = 0;
        String thisLevel = "";
        String nextLevel = "";
        switch (level) {
            case 1:
                drawable = R.drawable.img_dl_1;
                thisLevelDrawable = R.drawable.img_star_1;
                nextLevelDrawable = R.drawable.img_star_2;
                thisLevel = "新手团长";
                nextLevel = "铂金团长";
                break;
            case 10:
                drawable = R.drawable.img_dl_2;
                thisLevelDrawable = R.drawable.img_star_2;
                nextLevelDrawable = R.drawable.img_star_3;
                thisLevel = "铂金团长";
                nextLevel = "钻石团长";
                break;
            case 20:
                drawable = R.drawable.img_dl_3;
                thisLevelDrawable = R.drawable.img_star_3;
                nextLevelDrawable = R.drawable.img_star_4;
                thisLevel = "钻石团长";
                nextLevel = "星耀团长";
                break;
            case 30:
                drawable = R.drawable.img_dl_4;
                thisLevelDrawable = R.drawable.img_star_4;
                nextLevelDrawable = R.drawable.img_star_4;
                thisLevel = "星耀团长";
                nextLevel = "星耀团长";
                break;
        }

        if (drawable == 0) {
            rlLevel.setVisibility(View.GONE);
            ivLevel.setVisibility(View.GONE);
        } else {
            if (level >= 30) {
                rlLevel.setVisibility(View.GONE);
            } else {
                rlLevel.setVisibility(View.VISIBLE);
                if (model != null) {
                    seekBar.setMax(1000);
                    seekBar.setProgress((int) (model.getProgr() * (model.getProgr() > 1 ? 10 : 1000)));
                    tvLackPrice.setText(PriceUtils.getPriceText2(model.getDiff_quotas()));
                }
            }
            ivLevel.setVisibility(View.VISIBLE);
            Drawable drawable1 = getResources().getDrawable(thisLevelDrawable);
            drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
            tvThisLevel.setCompoundDrawables(null, drawable1, null, null);
            tvThisLevel.setText(thisLevel);
            Drawable drawable2 = getResources().getDrawable(nextLevelDrawable);
            drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
            tvNextLevel.setCompoundDrawables(null, drawable2, null, null);
            tvNextLevel.setText(nextLevel);
            ivLevel.setImageResource(drawable);
        }
    }


    private void setServiceView() {
        Drawable drawable = null;
        if (level > 0) {
            rl1.setText("我的团队");
            drawable = getResources().getDrawable(R.drawable.img_my_invite);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl1.setCompoundDrawables(null, drawable, null, null);

            rl2.setText("我要推广");
            drawable = getResources().getDrawable(R.drawable.img_share);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl2.setCompoundDrawables(null, drawable, null, null);

            rl3.setText("我的地址");
            drawable = getResources().getDrawable(R.drawable.img_my_address);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl3.setCompoundDrawables(null, drawable, null, null);

            rl4.setText("售前客服");
            drawable = getResources().getDrawable(R.drawable.img_my_kefu);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl4.setCompoundDrawables(null, drawable, null, null);

            rl5.setText("售后客服");
            drawable = getResources().getDrawable(R.drawable.img_my_service_hou);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl5.setCompoundDrawables(null, drawable, null, null);

            rl6.setText("意见反馈");
            drawable = getResources().getDrawable(R.drawable.img_my_feedback);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl6.setCompoundDrawables(null, drawable, null, null);

            rl7.setText("设置");
            drawable = getResources().getDrawable(R.drawable.img_my_set);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl7.setCompoundDrawables(null, drawable, null, null);

            rl6.setVisibility(View.VISIBLE);
            rl7.setVisibility(View.VISIBLE);
        } else {
            rl6.setVisibility(View.INVISIBLE);
            rl7.setVisibility(View.INVISIBLE);
            rl1.setText("我的地址");
            drawable = getResources().getDrawable(R.drawable.img_my_address);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl1.setCompoundDrawables(null, drawable, null, null);

            rl2.setText("售前客服");
            drawable = getResources().getDrawable(R.drawable.img_my_kefu);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl2.setCompoundDrawables(null, drawable, null, null);

            rl3.setText("售后客服");
            drawable = getResources().getDrawable(R.drawable.img_my_service_hou);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl3.setCompoundDrawables(null, drawable, null, null);

            rl4.setText("意见反馈");
            drawable = getResources().getDrawable(R.drawable.img_my_feedback);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl4.setCompoundDrawables(null, drawable, null, null);

            rl5.setText("设置");
            drawable = getResources().getDrawable(R.drawable.img_my_set);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            rl5.setCompoundDrawables(null, drawable, null, null);


        }

    }


    public void refreshMsgCount(int count) {
        if (tvMessageCount != null) {
            if (count > 0) {
                tvMessageCount.setVisibility(View.VISIBLE);
                tvMessageCount.setText(count > 99 ? "99+" : String.valueOf(count));
            } else tvMessageCount.setVisibility(View.GONE);
        }
    }

    public void refreshMoney(SaleModel saleModel) {
        if (saleModel != null) {
            tvMoney0.setText(PriceUtils.getPriceText2(saleModel.getToday_predict_rebeta()));
            tvMoney1.setText(PriceUtils.getPriceText2(saleModel.getNow_month_predict_quotas()));
            tvMoney2.setText(PriceUtils.getPriceText2(saleModel.getNow_month_predict_rebeta()));
            tvMoney3.setText(PriceUtils.getPriceText2(saleModel.getTotal_quotas()));
            tvMoney4.setText(PriceUtils.getPriceText2(saleModel.getTotal_rebeta()));
        }
    }

    String inviteCode;

    public void setUser(UserInfoModel userInfoModel, LevelupModel levelupModel) {
        String loginNickName = userInfoModel != null ? userInfoModel.getNickname() : (String) SharedPreferencesUtils.get(getActivity(), SharedPreferencesUtils.SpEnum.LoginNickName, "");
        tvNickName.setText(loginNickName);
        String headImg = userInfoModel != null ? userInfoModel.getAvatar() : (String) SharedPreferencesUtils.get(getActivity(), SharedPreferencesUtils.SpEnum.LoginHeadImg, "");
        HCUtils.loadWebImg(getContext(), ivHeadImg, headImg);
        int level = userInfoModel != null ? userInfoModel.getLevel() : (int) SharedPreferencesUtils.get(getActivity(), SharedPreferencesUtils.SpEnum.LoginUserLevel, 0);
        setLevel(level, levelupModel);

        inviteCode = userInfoModel != null ? userInfoModel.getInvite_code() : (String) SharedPreferencesUtils.get(getActivity(), SharedPreferencesUtils.SpEnum.InviteCode, "");
        if (TextUtils.isEmpty(inviteCode)) {
            inviteCode = "—";
            tvInviteCopy.setVisibility(View.GONE);
        } else {
            tvInviteCopy.setVisibility(View.VISIBLE);
        }
        tvInviteCode.setText(inviteCode);
        String referee = userInfoModel != null ? userInfoModel.getReferee() : (String) SharedPreferencesUtils.get(getActivity(), SharedPreferencesUtils.SpEnum.Referee, "");
        boolean isShowMyParent = true;
        if (TextUtils.isEmpty(referee)) {
            referee = "—";
            ivMyParent.setVisibility(View.GONE);
        } else {
            ivMyParent.setVisibility(View.VISIBLE);
            isShowMyParent = (boolean) SharedPreferencesUtils.get(getContext(), SharedPreferencesUtils.SpEnum.IsShowMyParent, true);
            ivMyParent.setImageResource(isShowMyParent ? R.drawable.img_my_parent_visi : R.drawable.img_my_parent_gone);
        }

        if (referee.length() > 6) {
            referee = referee.substring(0, 6) + "...";
        }


        tvMyParent.setTag(referee);
        if (isShowMyParent) tvMyParent.setText(referee);
        else tvMyParent.setText("****");
    }

    public void setOrder(OrderNumModel orderNumModel) {
        tvWaitPayCount.setVisibility(View.GONE);
        tvWaitSendCount.setVisibility(View.GONE);
        tvWaitGetCount.setVisibility(View.GONE);
        if (orderNumModel == null) return;
        if (orderNumModel.getWait_paid() > 0) {
            tvWaitPayCount.setVisibility(View.VISIBLE);
            tvWaitPayCount.setText(String.valueOf(orderNumModel.getWait_paid()));
        }

        if (orderNumModel.getWait_send() > 0) {
            tvWaitSendCount.setVisibility(View.VISIBLE);
            tvWaitSendCount.setText(String.valueOf(orderNumModel.getWait_send()));
        }

        if (orderNumModel.getWait_received() > 0) {
            tvWaitGetCount.setVisibility(View.VISIBLE);
            tvWaitGetCount.setText(String.valueOf(orderNumModel.getWait_received()));
        }
    }


}
